﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using IndoorPosition.Server.Core.Entities;
using IndoorPosition.Server.Core.Utils;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IndoorPosition.Server.Application
{
    /// <summary>
    /// 建筑物管理逻辑代码
    /// </summary>
    public class BuildingService : IBuildingService, ITransient
    {
        IRepository<Building> _buildingRepository;
        IRepository<Project> _projectRepository;
        public BuildingService(IRepository<Building> buildingRepository, IRepository<Project> projectRepository)
        {
            _buildingRepository = buildingRepository;
            _projectRepository = projectRepository;
        }

        /// <summary>
        /// 添加建筑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(BuildingInputDto input)
        {
            var existProject = await _projectRepository.Where(p => p.Id == input.ProjectId).AnyAsync();
            if (!existProject)
            {
                throw Oops.Oh("项目ID不正确");
            }
            var building = input.Adapt<Building>();
            if (input.Model != null)
            {
                building.Model = await FileUtil.SaveFileAsync(input.Model, "building");
            }
            var result = await _buildingRepository.InsertNowAsync(building);
            return result.Entity.Id;
        }

        /// <summary>
        /// 删除建筑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _buildingRepository.DeleteNowAsync(id);
        }

        /// <summary>
        /// 根据ID获取建筑物信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BuildingDto> GetAsync(int id)
        {
            var building = await _buildingRepository.Where(b => b.Id == id).Include("Project").Include("Floors").FirstOrDefaultAsync();
            return building.Adapt<BuildingDto>();
        }

        /// <summary>
        /// 分页获建筑信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedList<BuildingDto>> ListAsync(PageInputDto input)
        {
            var query = _buildingRepository.AsQueryable();
            if (!string.IsNullOrEmpty(input.Keyword))
            {
                query = query.Where(p => p.Name.Contains(input.Keyword));
            }
            if (input.Conditions != null && input.Conditions.Count > 0)
            {
                query = query.Where(LambdaExpressionBuilder.BuildLambda<Building>(input.Conditions));
            }
            var buildingPageList = await query.Include("Project").Include("Floors").ToPagedListAsync(input.Page, input.Size);
            return buildingPageList.Adapt<PagedList<BuildingDto>>();
        }

        /// <summary>
        /// 下载模型文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task<IActionResult> DownloadModelAsync(string fileName)
        {
            return new FileStreamResult(FileUtil.GetStream(fileName, "building"), "model/gltf-binary") { FileDownloadName = fileName };
        }
    }
}
